package com.eece496.Server;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.eece496.Database.Enrollment;
import com.eece496.Database.Student;
import com.eece496.Database.TAEnrollment;
import com.eece496.Database.TeachingAssistant;
import com.eece496.Helpers.StudentInfo;
import com.eece496.Helpers.TAInfo;

/**
 * Servlet implementation class ProfAddStudentTAServlet
 */
public class ProfAddStudentTAServlet extends MasterServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfAddStudentTAServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Connect();
		
		String ButtonClicked = request.getParameter("button");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String courseNumber = request.getParameter("courseNumber");
		String sectionNumber = request.getParameter("sectionNumber");
		System.out.println(courseNumber);
		System.out.println(sectionNumber);
		if (ButtonClicked.equals("Add Student")) {
			
			
			String studentID = request.getParameter("studentID");
			String firstName = request.getParameter("firstName");
			String lastName = request.getParameter("lastName");
			String email = request.getParameter("email");
			String program = request.getParameter("program");
			String year = request.getParameter("year");
			String GPA = request.getParameter("gpa");
			
			System.out.println(firstName);
			System.out.println(lastName);
			int sID = Integer.parseInt(studentID);
			int yr = Integer.parseInt(year);
			int gpa = Integer.parseInt(GPA);
			
			Student StudentDB = new Student(sID, "lala", "password", firstName,
					lastName, email, program, yr, gpa);
			StudentDB.Setup(connection);
			
			try {
				if(StudentDB.UserExists(sID) == false)
				{
					StudentDB.Insert();
				}
	
			Enrollment EnrollmentDB = new Enrollment(sID, courseNumber, sectionNumber);
			EnrollmentDB.Setup(connection);
			EnrollmentDB.Insert();
			

			TAEnrollment TAEnrollmentDB = new TAEnrollment();
			TAEnrollmentDB.Setup(connection);

			ArrayList<StudentInfo> sInfo = new ArrayList<StudentInfo>();
			ArrayList<TAInfo> tInfo = new ArrayList<TAInfo>();

			sInfo = EnrollmentDB.GetAllStudentsEnrolled(courseNumber,
					sectionNumber);
			tInfo = TAEnrollmentDB.GetAllTAsEnrolled(courseNumber,
					sectionNumber);
			
			request.setAttribute("courseNumber", courseNumber);
			request.setAttribute("sectionNumber", sectionNumber);
			request.setAttribute("StudentInfo", sInfo);
			request.setAttribute("TAInfo", tInfo);
			request.setAttribute("firstname", firstname);
			request.setAttribute("lastname", lastname);
			request.getRequestDispatcher("/Professor/Professor-View-Class-List.jsp")
					.forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		else if(ButtonClicked.equals("Add TA"))
		{
			String TAID = request.getParameter("taid");
			String firstName = request.getParameter("firstname");
			String lastName = request.getParameter("lastname");
			String email = request.getParameter("email");
			System.out.println(TAID);
			System.out.println(firstName);
			System.out.println(lastName);
			System.out.println(email);
			int tID = Integer.parseInt(TAID);
			
			TeachingAssistant TADB = new TeachingAssistant(tID, "kjkj", "password", firstName,
					lastName, email);
			TADB.Setup(connection);
			
			try {
				if(TADB.UserExists(tID) == false)
				{
					TADB.Insert();
				}
	
			TAEnrollment TAEnrollmentDB = new TAEnrollment(tID, courseNumber, sectionNumber);
			TAEnrollmentDB.Setup(connection);
			TAEnrollmentDB.Insert();
			
			Enrollment EnrollmentDB = new Enrollment();
			EnrollmentDB.Setup(connection);

			ArrayList<StudentInfo> sInfo = new ArrayList<StudentInfo>();
			ArrayList<TAInfo> tInfo = new ArrayList<TAInfo>();

			sInfo = EnrollmentDB.GetAllStudentsEnrolled(courseNumber,
					sectionNumber);
			tInfo = TAEnrollmentDB.GetAllTAsEnrolled(courseNumber,
					sectionNumber);
			
			request.setAttribute("courseNumber", courseNumber);
			request.setAttribute("sectionNumber", sectionNumber);
			request.setAttribute("StudentInfo", sInfo);
			request.setAttribute("TAInfo", tInfo);
			request.setAttribute("firstname", firstname);
			request.setAttribute("lastname", lastname);
			request.getRequestDispatcher("/Professor/Professor-View-Class-List.jsp")
					.forward(request, response);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
	}

}
